package com.web.controller;

import com.jfinal.core.Controller;
import com.web.model.PageTitleImage;
import com.web.util.Properties;
import com.web.util.aliyun.ReplaceImgURL;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.List;

/**
 * Created by dengcunbin on 16/1/13.
 */
public class DataBaseOperatController extends Controller {
    private static final Logger LOGGER = Logger.getLogger(DataBaseOperatController.class);

    public void updateDBImageURL() throws IOException {

        List<PageTitleImage> pageTitleImageList = PageTitleImage.pageTitleImageDao.getAllTitleImage();

        for (PageTitleImage pageTitleImage : pageTitleImageList) {
            String sourceImageURL = "";
            String sourceImage2URL = "";
            // 更新imageSrc字段
            if (pageTitleImage.getStr("imageSrc") != null) {
                if (pageTitleImage.getStr("imageSrc").contains("kuibu")) {
                    sourceImageURL = pageTitleImage.getStr("imageSrc");
                } else {
                    sourceImageURL = Properties.SaveDir + pageTitleImage.getStr("imageSrc");
                }
                String newImageSrcURL = ReplaceImgURL.replaceTitleUrl(sourceImageURL);
                pageTitleImage.set("imageSrc", newImageSrcURL);
            }

            // 更新imageSrc2字段
            if (pageTitleImage.getStr("imageSrc2") != null) {
                if (pageTitleImage.getStr("imageSrc2").contains("kuibu")) {
                    sourceImage2URL = pageTitleImage.getStr("imageSrc2");
                } else {
                    sourceImage2URL = Properties.SaveDir + pageTitleImage.getStr("imageSrc2");
                }
                String newImageSrc2URL = ReplaceImgURL.replaceTitleUrl(sourceImage2URL);
                pageTitleImage.set("imageSrc2", newImageSrc2URL);
            }
        }

        PageTitleImage.pageTitleImageDao.updateTitleImage(pageTitleImageList);


        setAttr("updateDBImageURLResult", true);
        renderJson();
    }
}
